package at.fhooe.ems.util;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import at.fhooe.ems.dal.ClientRequestModel;
import at.fhooe.ems.dal.ClientResponseModel;
import at.fhooe.ems.dal.SensorDataModel;

import com.google.gson.Gson;

public class DataUtil {
	
	public static String convertToJSON(SensorDataModel model){
	  SimpleDateFormat sdf = new SimpleDateFormat("MMMM dd, yyyy hh:mm:ss aaa", Locale.ENGLISH);
	  Gson gson=new Gson();
	  List<String> activeSensors = model.getActiveSensors();
	  String jsonBegin = "";
	  if(activeSensors==null){
		  jsonBegin+="{sensorTime:\""+ sdf.format(model.getSensorTime())+"\","+
				  "\"temperature\":NULL,"+
				  "\"humidity\":NULL,"+
				  "\"smoke\":NULL,"+
				  "\"lightness\":NULL,"+
				  "\"noise\":NULL,"+
				  "\"smokeConstant\":NULL}";
	  }else{
		  for(String s : activeSensors){
			  jsonBegin = "{sensorTime:\""+ sdf.format(model.getSensorTime())+"\",";
			  if(activeSensors.contains("Temperature")){
		    	  jsonBegin+="\"temperature\":"+model.getTemperature()+",";
			  }else{
				  jsonBegin+="\"temperature\":NULL,";
			  }
			  if(activeSensors.contains("Humidity")){
		    	  jsonBegin+="\"humidity\":"+model.getHumidity()+",";	
			  }else{
		    	  jsonBegin+="\"humidity\":NULL,";	 
			  }
			  if(activeSensors.contains("Smoke")){
				  if(model.getSmokeConstant()>0){
					  jsonBegin+="\"smoke\":true,";
				  }else{
					  jsonBegin+="\"smoke\":false,";
				  }
			  }else{
		    	  jsonBegin+="\"smoke\":NULL,";	
			  }
			  if(activeSensors.contains("Light")){
		    	  jsonBegin+="\"lightness\":"+model.getLightness()+",";	 
			  }else{
		    	  jsonBegin+="\"lightness\":NULL,";
			  }
			  if(activeSensors.contains("Noise")){
		    	  jsonBegin+="\"noise\":"+model.getNoise()+",";	
			  }else{
		    	  jsonBegin+="\"noise\":NULL,";
			  }
			  if(activeSensors.contains("Smoke")){
		    	  jsonBegin+="\"smokeConstant\":"+model.getSmokeConstant();	
			  }else{
		    	  jsonBegin+="\"smokeConstant\":NULL";
			  }
			  jsonBegin+="}";
		  }
	  }
	  //return gson.toJson(model);
	  return jsonBegin;
	}
	
	public static SensorDataModel convertJSONtoSensorModel(String JSON){
		SensorDataModel data=new SensorDataModel();
		Gson gson=new Gson();
		data = gson.fromJson(JSON,data.getClass());
		return data;
	}
	
	public static ClientRequestModel convertCSVToRequestModel(String csv){
		ClientRequestModel request = new ClientRequestModel();
		String[] data = csv.split("\\*");
		try {
			//request.setDateFrom(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(data[0]));
			//request.setDateTo(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(data[1]));
			request.setDateFrom(data[0]);
			request.setDateTo(data[1]);
			request.setType(data[2]);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return request;
		
	}
	
    public static String converDateToString(Date date ){
    	SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    	return df.format(date); 
    }
    
    public static String convertResponseToJson(ArrayList<ClientResponseModel> response){
    	String beginJsonArray="{\"clientresponse\":[";
    	String endJsonArray = "]}";
    	String middleJsonArray="";
    	
    	for(int i=0; i<response.size(); i++){
    		middleJsonArray+="{\"sensor_time\":\""+response.get(i).getRedordDateTime()+"\",";
    		if(response.size()-1!=i)
    			middleJsonArray+="\""+response.get(i).getType()+"\":\""+response.get(i).getValue()+"\"},";
    		else
    			middleJsonArray+="\""+response.get(i).getType()+"\":\""+response.get(i).getValue()+"\"}";
    	}
    	
    	return beginJsonArray+middleJsonArray+endJsonArray;
    }
    
}
